<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <button>点击</button>
  </body>
  <script>
    // let myname = "name";
    // let obj={
    //     [myname]:"zhangsan"
    // }
    // console.log(obj);

    // let obj = {
    //     myname:"张三",
    //     name:'lisi'
    // }
    // let myname = "name";
    // console.log(obj[myname]);

    //节流
    // function throttle(fn,delay){
    //     //连续触发只触发一次
    //     let timer = new Date().getTime();
    //     return function(){
    //         let now = new Date().getTime();
    //         if(now-timer>delay){
    //             fn()
    //             timer=new Date().getTime();
    //         }

    //     }
    // }
    // document.querySelector("button").onclick=throttle(function(){
    //     console.log("点击了");
    // },3000)

    // 防抖
    function debounce(fn,delay){
        let timer = null;
        return function(){
            if(!timer){
                fn()
            }
            clearTimeout(timer);
            timer=setTimeout(() => {
                timer=null;
            }, delay);
        }
    }
    // function debounce(cb, wait) {
    //   let timer;
    //   return function () {
    //     clearTimeout(timer);
    //     timer = setTimeout(() => cb(), wait);
    //   };
    // }
    document.querySelector("button").onclick = debounce(function () {
      console.log("点击了");
    }, 3000);
  </script>
</html>
